/**
 * Created by Administrator on 2018/1/15.
 */
define(function(require, exports, module) {
    var $=require('zepto');
    cn=require('conment');
    api=require('api');
    module.exports = {
        int:{
            index:0,
            size:0,
            money:0,
            isLianPaySelf:true,
            lianPayTxt:'',
        },
        liansBindBank:function () {
            const _this=this;
            cn.Ajax(api.lianBindbank,{
                },function(d){
                    var data=d;
                    if(data.ret_code!='0000'){
                        $('#nolinacard').show();
                    }else if(data.agreement_list.length==0){
                        $('#nolinacard').show();
                    }else{
                        $('#yeslinacard').show();
                        $('#lianlianhuan').show();
                        var list=data.agreement_list;
                        var arr=new Array();
                        $.each(list, function (i, l, array) {
                            arr.push(' <li class="'+(i==0?'backActive':'')+'"> <p class="bankNo"><input type="hidden" value="'+l.card_no+'"> <span class="xin">**** **** **** **** <i class="card_no">'+l.card_no+'</i></span> </p> </li>')
                            $('.card_no').text(list[0].card_no)
                        });
                        $('#model-bank-list').append(arr.join(''));
                        $('.card_no').text($('#model-bank-list li.backActive input').val())
                        $('#cardId').val($('#model-bank-list li.backActive input').val());
                        $('#model-bank-list li').on('click',function () {
                            $(this).addClass('backActive').siblings('li').removeClass('backActive');
                            $('.card_no').text($('#model-bank-list li.backActive input').val());
                            $('#cardId').val($('#model-bank-list li.backActive input').val());
                            $(".add-bank-list").animate({"left":"100%"},500);
                            $('#yeslinacard').show();
                            $('#nolinacard').hide();
                            _this.addCard=true
                        })

                    }
            },'post');
        },

        lianlianPay:function () {
            const _this=this;
            var bcn_t1=$('#extract_card').val();
            var bcn_t2=$('#cardId').val();
            if($('#yeslinacard').is(":visible")){
                var bcn_t=bcn_t2
            }else{
                var bcn_t=bcn_t1;
            }
            // if(!_this.addCard){
            //     var bcn_t=bcn_t1;
            // }else{
            //     var bcn_t=((bcn_t1==''||bcn_t1==null)?bcn_t2:bcn_t1);
            // }
            if(bcn_t1==''&&bcn_t2==''){
                cn.alert('请输入银行卡号')
                return
            }
            if(!_this.addCard&&(bcn_t1==''||bcn_t1==null)){
                cn.alert('请输入银行卡号')
                return
            }
            var money_new=Number($('#charge-money').val());
            _this.int.money=money_new;
            if(_this.int.money==0){
                cn.alert('请输入充值金额')
                return
            }
            // var bcn_t=((bcn_t1==''||bcn_t1==null)?bcn_t2:bcn_t1);
            // cn.dialog({
            //     body:'确认使用卡号'+bcn_t+'充值',
            //     close:'取消',
            //     confirm:{
            //         text:'充值',
            //         callBack:function () {
                        cn.Ajax(api.lianlianPay,{
                            amount:_this.int.money,
                            // bci:bcn_t,
                            bcn:bcn_t,
                        },function(d){
                            if(d.flag==1){
                                $('#lianlianDataUrl').attr('action',d.req_url);
                                $('#lianlianDateVal').val(d.req_data);
                                $('#lianlianDataUrl').submit();
                            }else{
                                cn.alert(d.message)
                            }
                        },'post')
            //         }
            //     }
            // })

        },
        addCard:true,
        init:function () {
            const _this=this;
            _this.liansBindBank();
            $('#charge-money').on('input porpertychange',function () {
                var money_new=$(this).val();
                _this.int.money=money_new;
                $('#pay_lian_money').text(_this.int.money);
            });

            $('#back-bank-list').on('click',function () {
                $(".add-bank-list").animate({"left":"100%"},500)
            });
            $('#lianlianhuan').on('click',function () {
                $(".add-bank-list").animate({"left":"0%"},500)
            })
            $('#lian_btn').on('click',function () {
                _this.lianlianPay();
            })
            $('.add-bank').on('click',function () {
                $(".add-bank-list").animate({"left":"100%"},500)
                $('#nolinacard').show();
                $('#yeslinacard').hide();
                _this.addCard=false;
            })
        },
    }

});